package com.org.khtn.command.select;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.google.common.collect.Multimap;

public class Command {
	public Map<String, Map<String, Map<String, String>>> data;
	Map<String, Multimap<String, String>> resultData;
	String[] tables;
	final String className = "Commmand:";
	Map<String, Map<String, String>> tableDefine;
	String databaseName;

	public void setTables(String[] Tables) {
		tables = Tables;

	}

	Vector<String> currentTable;

	public void putCurrentTables(String table) {
		currentTable.add(table);
	}

	public void putTableDefine(String tableName, Map<String, String> Data) {
		tableDefine.put(tableName, Data);
	}

	public Command() {
		resultData = new HashMap<String, Multimap<String, String>>();
		data = new HashMap<String, Map<String, Map<String, String>>>();
		tableDefine = new HashMap<String, Map<String, String>>();
		currentTable = new Vector<String>();
	}

	public void setData(Map<String, Map<String, Map<String, String>>> Data) {

		final String methodName = "setData():";
		// System.out.println(className+methodName+"Data: "+Data);
		if (data == null)
			return;
		data.clear();
		data = new HashMap<String, Map<String, Map<String, String>>>();
		for (Map.Entry<String, Map<String, Map<String, String>>> e : Data
				.entrySet()) {

			data.put(e.getKey(), e.getValue());
		}

	}

	public void setTableDefine(Map<String, Map<String, String>> Data) {
		tableDefine = new HashMap<String, Map<String, String>>();
		tableDefine.putAll(Data);
	}

	public Boolean ambiguousField(String fieldName) {
		final String methodName = "ambiguousField():";
		// System.out.println(className+methodName+"0");

		if (resultData == null)
			return true;
		// System.out.println(className+methodName+"1");
		// System.out.println(className+methodName+"fieldName:"+fieldName);

		for (Map.Entry<String, Multimap<String, String>> d : resultData
				.entrySet()) {
			Multimap<String, String> temp = d.getValue();
			boolean flags = false;
			for (Map.Entry<String, String> ddd : temp.entries()) {
				if (ddd.getKey().equalsIgnoreCase(fieldName))
					flags = true;
			}
			if (!flags)
				return true;
			// System.out.println(className+methodName+"2");

			List<String> value = new ArrayList<String>();
			value = (List<String>) temp.get(fieldName);
			// System.out.println(className+methodName+"value:"+value);
			if (value.size() > 1)
				return true;
		}
		return false;
	}

	public void putData(Map<String, Multimap<String, String>> Data) {
		resultData.clear();
		resultData = new HashMap<String, Multimap<String, String>>();
		for (Map.Entry<String, Multimap<String, String>> e : Data.entrySet()) {

			resultData.put(e.getKey(), e.getValue());
		}
	}

	public Vector getAllResultDataRowID() {
		Vector result1 = new Vector<>();
		for (Map.Entry<String, Multimap<String, String>> e : resultData
				.entrySet()) {

			result1.add(e.getKey());
		}
		return result1;
	}

	public void putAllCurrentTable(Vector table) {
		currentTable.addAll(table);
	}

	public Map<String, Multimap<String, String>> getResultData() {
		return resultData;
	}

	public Map<String, Map<String, String>> getTable(String tableName)

	{
		if (data.containsKey(tableName))
			return data.get(tableName);
		return null;
	}

	public void setDatabaseName(String DatabaseName) {
		databaseName = DatabaseName;
	}

	public Boolean tableExist(String name) {
		final String methodName = "tableExist():";
		System.out.println(className + methodName + "table.length"
				+ tables.length);
		for (String s : currentTable) {
			System.out.println(s);
			if (s.equalsIgnoreCase(name))
				return true;
		}
		return false;
	}

}
